package com.epsilon.pos.principal.ventas.delegator;

import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.widgets.Table;

import com.epsilon.commons.context.model.ContextoDeAplicacion;
import com.epsilon.commons.util.WidgetsUtil;
import com.epsilon.pos.principal.ventas.delegate.VentasPrincipalEventDelegate;
import com.epsilon.pos.principal.ventas.model.VentasPrincipalException;
import com.epsilon.pos.principal.ventas.view.VentasPrincipalComposite;
import com.epsilon.pos.principal.ventas.viewers.VentasPrincipalTableConfig;
import com.epsilon.pos.productos.delegator.ProductoDescripcionControl;

public class VentasPrincipalControl {
	public static final int _CAJA_ = 1;
	
	private VentasPrincipalEventDelegate ventasPrincipalEventDelegate;
	
	private VentasPrincipalControl( VentasPrincipalEventDelegate ventasPrincipalEventDelegate ) {
		this.ventasPrincipalEventDelegate = ventasPrincipalEventDelegate;
	}
	
	public static VentasPrincipalControl getInstance( VentasPrincipalComposite ventasPrincipalComposite, ContextoDeAplicacion contexto ) throws VentasPrincipalException {

		ProductoDescripcionControl productoDescripcionControl = ProductoDescripcionControl.getInstance( ventasPrincipalComposite.getProductoDescripcionComposite() );
		VentasPrincipalEventDelegate eventos = new VentasPrincipalEventDelegate( ventasPrincipalComposite, contexto );
		eventos.setProductoDescripcionControl( productoDescripcionControl );

		setEvents( ventasPrincipalComposite, eventos );
		
		return new VentasPrincipalControl( eventos );
	}
	
	private static void setEvents( VentasPrincipalComposite composite, VentasPrincipalEventDelegate events ) {
		composite.getBtnAgregarArticulo().addSelectionListener( events.getEventoBtnAgregarArticulo() );
		composite.getBtnBusqueda().addSelectionListener( events.getEventoBtnBusqueda() );
		composite.getBtnCancelarArticulo().addSelectionListener( events.getEventoBtnCancelarArticulo() );
		composite.getBtnConcretarVenta().addSelectionListener( events.getEventoBtnConcretarVenta() );
		composite.getBtnCancelarVenta().addSelectionListener( events.getEventoBtnCancelarVenta() );
		composite.getTxtCodigo().addKeyListener( events.getEventoTxtCodigo() );
		composite.getTblVentas().addSelectionListener( events.getEventoTblDeProductos() );
		composite.getTblVentas().addKeyListener( events.getEventoKeyTblProductos() );
		composite.getTblVentas().addMouseListener( events.getEventoClickTblProductos() );
		composite.getBtnCantidad().addSelectionListener( events.getEventoBtnCantidad() );
		composite.getTxtTicket().addKeyListener( events.getEventoTxtTicket() );
		composite.getTxtTicket().addVerifyListener( WidgetsUtil.getVerifyListenerCampoNumericoEntero( composite.getTxtTicket() ) );
		composite.getBtnImprimirTicket().addSelectionListener( events.getEventoBtnImprimirTicket() );
		composite.getBtnCortesia().addSelectionListener( events.getEventoBtnCortesia() );
		composite.getBtnCorteDeCaja().addSelectionListener( events.getEventoBtnCorte() );
		
		composite.getBtn1().addSelectionListener( events.new EventoBtnNumerado( "2" ) );
		composite.getBtn2().addSelectionListener( events.new EventoBtnNumerado( "2" ) );
		composite.getBtn3().addSelectionListener( events.new EventoBtnNumerado( "2" ) );
		composite.getBtn4().addSelectionListener( events.new EventoBtnNumerado( "2" ) );
		composite.getBtn5().addSelectionListener( events.new EventoBtnNumerado( "2" ) );
		composite.getBtn6().addSelectionListener( events.new EventoBtnNumerado( "2" ) );
		composite.getBtn7().addSelectionListener( events.new EventoBtnNumerado( "2" ) );
		composite.getBtn8().addSelectionListener( events.new EventoBtnNumerado( "2" ) );
		composite.getBtn9().addSelectionListener( events.new EventoBtnNumerado( "2" ) );
		composite.getBtn10().addSelectionListener( events.new EventoBtnNumerado( "2" ) );
		composite.getBtn11().addSelectionListener( events.new EventoBtnNumerado( "2" ) );
		composite.getBtn12().addSelectionListener( events.new EventoBtnNumerado( "2" ) );
		composite.getBtn13().addSelectionListener( events.new EventoBtnNumerado( "2" ) );
		composite.getBtn14().addSelectionListener( events.new EventoBtnNumerado( "2" ) );
		
		events.setVwTblProductos( getConfiguracionTblProductos( composite.getTblVentas() ) );
	}
	
	private static TableViewer getConfiguracionTblProductos( Table tblDeProductos ) {
		
		TableViewer vwTblDeProductos = new TableViewer( tblDeProductos );
		
		VentasPrincipalTableConfig principalConfig = new VentasPrincipalTableConfig();
		vwTblDeProductos.setContentProvider( principalConfig );
		vwTblDeProductos.setLabelProvider( principalConfig );
		
		return vwTblDeProductos;
	}
	
	public VentasPrincipalComposite getVentasPrincipalComposite() {
		return ventasPrincipalEventDelegate.getVentasPrincipalComposite();
	}
	
	public TableViewer getVwTblProductos() {
		return ventasPrincipalEventDelegate.getVwTblProductos();
	}
	
	public ProductoDescripcionControl getProductoDescripcionControl() {
		return ventasPrincipalEventDelegate.getProductoDescripcionControl();
	}
	
	public void refreshVentasPrincipal() {
		ventasPrincipalEventDelegate.refreshVentasPrincipal();
	}
}